import { queryOrder, addOrder, removeOrder, queryOrderEnums, changeStatus } from '../services/api';

export default {
  namespace: 'order',

  state: {
    data: {
      content: [],
      totalElements: '',
      size: '',
      number: '',
    },
    loading: true,
    enums: {},
    ordersForSelect: [],
  },

  effects: {
    *changeStatus({ payload }, { call, put }) {
      yield call(changeStatus, payload);
      const response = yield call(queryOrder);
      yield put({
        type: 'save',
        payload: response.data,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *saveSelected({ payload }, { put }) {
      yield put({
        type: 'select',
        payload,
      });
    },
    *fetchEnums({ payload }, { call, put }) {
      const response = yield call(queryOrderEnums, payload);
      yield put({
        type: 'saveEnums',
        payload: response.data,
      });
    },
    *fetchForSelect({ payload }, { call, put }) {
      const response = yield call(queryOrder, payload);
      yield put({
        type: 'saveOrdersForSelect',
        payload: response.data.content,
      });
    },
    *fetch({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryOrder, payload);
      yield put({
        type: 'save',
        payload: response.data,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      yield call(addOrder, payload);
      const response = yield call(queryOrder);
      yield put({
        type: 'save',
        payload: response.data,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      yield put({
        type: 'current',
        payload: {},
      });

      if (callback) callback();
    },
    *remove({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      yield call(removeOrder, payload);
      const response = yield call(queryOrder);
      yield put({
        type: 'save',
        payload: response.data,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });

      if (callback) callback();
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveEnums(state, action) {
      return {
        ...state,
        enums: action.payload,
      };
    },
    select(state, action) {
      return {
        ...state,
        selectedRecord: action.payload,
      };
    },
    saveOrdersForSelect(state, action) {
      return {
        ...state,
        ordersForSelect: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
